var provinceType=['省内', '省外'];
var app = {
    provinceType: provinceType,
    //出差事由列表
    reasonList: [{
        reasonId: 1,
        value: '培训'
    }, {
        reasonId: 2,
        value: '会议'
    }, {
        reasonId: 3,
        value: '调研'
    }, {
        reasonId: 4,
        value: '招生宣传'
    }],
    //职务级别列表
    levelList: [{
        levelId: 1,
        type:'provinceLevel',
        value: '省部级'
    },
        {
            levelId: 2,
            type: 'cityLevel',
            value: '厅局级'
        },
        {
            levelId: 3,
            type: 'normalLevel',
            value: '普通人员'
        }
    ],
    //省级列表
    provinceList: ['陕西','北京', '天津', '河北', '山西', '内蒙古', '辽宁', '吉林', '黑龙江', '上海', '江苏', '浙江', '安徽', '福建', '江西', '山东', '河南', '湖北', '湖南', '广东', '广西', '海南', '重庆', '四川', '贵州', '云南', '西藏', '甘肃', '青海', '宁夏', '新疆'],
    //陕西省市级列表

    cityList: ['西安市', '咸阳市', '铜川市', '宝鸡市', '渭南市', '汉中市', '商洛市', '安康市', '榆林市', '延安市', '杨凌区', '韩城市'],
    cityStayStandardList: [
        {
            city: '西安市',
            standard: {
                provinceLevel: 800,
                cityLevel: 460,
                normalLevel: 350
            }
        },
        {
            city: '咸阳市',
            standard: {
                provinceLevel: 600,
                cityLevel: 320,
                normalLevel: 260
            }
        }, {
            city: '铜川市',
            standard: {
                provinceLevel: 600,
                cityLevel: 300,
                normalLevel: 230
            }
        }, {
            city: '宝鸡市',
            standard: {
                provinceLevel: 600,
                cityLevel: 320,
                normalLevel: 260
            }
        }, {
            city: '渭南市',
            standard: {
                provinceLevel: 600,
                cityLevel: 300,
                normalLevel: 260
            }
        }, {
            city: '汉中市',
            standard: {
                provinceLevel: 600,
                cityLevel: 300,
                normalLevel: 230
            }
        }, {
            city: '商洛市',
            standard: {
                provinceLevel: 600,
                cityLevel: 300,
                normalLevel: 230
            }
        }, {
            city: '安康市',
            standard: {
                provinceLevel: 600,
                cityLevel: 300,
                normalLevel: 230
            }
        }, {
            city: '榆林市',
            standard: {
                provinceLevel: 680,
                cityLevel: 350,
                normalLevel: 300
            }
        }, {
            city: '延安市',
            standard: {
                provinceLevel: 680,
                cityLevel: 350,
                normalLevel: 300
            }
        }, {
            city: '杨凌区',
            standard: {
                provinceLevel: 680,
                cityLevel: 320,
                normalLevel: 260
            }
        }, {
            city: '韩城市',
            standard: {
                provinceLevel: 600,
                cityLevel: 300,
                normalLevel: 260
            }
        }
    ],
    //省外住宿标准
    provinceStayStandardList: [
        {
            province: '北京',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 1100,//省级
                cityLevel: 650,//市级
                normalLevel: 500 //普通
            }
        },
        {
            province: '天津',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 480,
                normalLevel: 380
            }
        },
        {
            province: '河北',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 450,
                normalLevel: 350
            }
        },
        {
            province: '山西',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 480,
                normalLevel: 350
            }
        },
        {
            province: '内蒙古',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 460,
                normalLevel: 350
            }
        },
        {
            province: '辽宁',
            type: 2,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {//淡季标准
                provinceLevel: 800,
                cityLevel: 480,
                normalLevel: 350
            },
            standardCity: {//特殊城市淡季标准
                provinceLevel: 800,
                cityLevel: 490,
                normalLevel: 350
            },
            standardCitySpecial: {//特殊城市旺季标准
                provinceLevel: 960,
                cityLevel: 590,
                normalLevel: 420
            },
            city: '大连',
            scale: 0.2,//旺季上浮比例
            months: [7, 8, 9]//旺季涉及月份
        }, {
            province: '吉林',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 450,
                normalLevel: 350
            }
        }, {
            province: '黑龙江',
            type: 1,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 450,
                normalLevel: 350
            },
            standardSpecial: {//特殊省份旺季住宿标准
                provinceLevel: 960,
                cityLevel: 450,
                normalLevel: 420
            },
            scale: 0.2,
            months: [7, 8, 9]
        }, {
            province: '上海',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 1100,
                cityLevel: 600,
                normalLevel: 500
            }
        }, {
            province: '江苏',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 900,
                cityLevel: 490,
                normalLevel: 380
            }
        }, {
            province: '浙江',
            type: 2,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 900,
                cityLevel: 500,
                normalLevel: 400
            },
            standardCity: {
                provinceLevel: 800,
                cityLevel: 450,
                normalLevel: 350
            },
            city: '宁波',
            scale: 0,
            months: []

        }, {
            province: '安徽',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 460,
                normalLevel: 350
            }
        }, {
            province: '福建',
            type: 2,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 900,
                cityLevel: 480,
                normalLevel: 380
            },
            standardCity: {
                provinceLevel: 900,
                cityLevel: 500,
                normalLevel: 400
            },
            city: '厦门',
            scale: 0,
            months: []

        }, {
            province: '江西',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 470,
                normalLevel: 350
            }
        }, {
            province: '山东',
            type: 2,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 480,
                normalLevel: 380
            },
            standardCity: {
                provinceLevel: 800,
                cityLevel: 490,
                normalLevel: 380
            },
            standardCitySpecial: {//特殊城市旺季标准
                provinceLevel: 960,
                cityLevel: 590,
                normalLevel: 450
            },
            city: '青岛',
            scale: 0.2,
            months: [7, 8, 9]
        }, {
            province: '河南',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 900,
                cityLevel: 480,
                normalLevel: 380
            }
        }, {
            province: '湖北',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 480,
                normalLevel: 350
            }
        }, {
            province: '湖南',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 450,
                normalLevel: 350
            }
        }, {
            province: '广东',
            type: 2,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 900,
                cityLevel: 550,
                normalLevel: 550
            },
            standardCity: {
                provinceLevel: 900,
                cityLevel: 550,
                normalLevel: 550
            },
            city: '深圳',
            scale: 0,
            months: []

        }, {
            province: '广西',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 470,
                normalLevel: 350
            }
        }, {
            province: '海南',
            type: 1,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 500,
                normalLevel: 350
            },
            standardSpecial: {
                provinceLevel: 1040,
                cityLevel: 650,
                normalLevel: 450
            },
            scale: 0.3,
            months: [11, 12, 1, 2]
        }, {
            province: '重庆',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 480,
                normalLevel: 370
            }
        }, {
            province: '四川',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 900,
                cityLevel: 470,
                normalLevel: 370
            }
        }, {
            province: '贵州',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 470,
                normalLevel: 370
            }
        }, {
            province: '云南',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 900,
                cityLevel: 480,
                normalLevel: 380
            }
        }, {
            province: '西藏',
            type: 1,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 500,
                normalLevel: 350
            },
            standardSpecial: {
                provinceLevel: 1200,
                cityLevel: 750,
                normalLevel: 530
            },
            scale: 0.5,
            months: [6, 7, 8, 9]
        }, {
            province: '甘肃',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 470,
                normalLevel: 350
            }
        }, {
            province: '青海',
            type: 1,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 500,
                normalLevel: 350
            },
            scale: 0.5,
            months: [6, 7, 8, 9]
        }, {
            province: '宁夏',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 470,
                normalLevel: 350
            }
        }, {
            province: '新疆',
            type: 0,  //0:全省正常省份 1：有特殊省份 2：有特殊城市
            standard: {
                provinceLevel: 800,
                cityLevel: 480,
                normalLevel: 350
            }
        }
    ],
    //伙食补助标准
    foodStandardList:[
        {
            id:1,
            provinceType: provinceType[0],//省内
            value:50 //每人每天50
        },
        {
            id: 2,
            provinceType: provinceType[1],//省外
            value: 80 //每人每天80
        }
    ],
    //交通补助标准
    trafficMoneyList: [
        {
            id: 1,
            provinceType: provinceType[0],//省内
            value: 80 //每人每天80
        },
        {
            id: 2,
            provinceType: provinceType[1],//省外
            value: 100 //每人每天100
        }
    ],
    //交通工具标准
    trafficStandardList: [{
        levelId: 1,//职务级别的id
        train: {
            normal: '软席（软座、软卧）',
            middle: '商务座',
            high: '一等软座'
        },
        ship: '一等舱',
        plane: '头等舱'
    },
        {
            levelId: 2,
            train: {
                normal: '软席（软座、软卧）',
                middle: '一等座',
                high: '一等软座'
            },
            ship: '二等舱',
            plane: '经济舱'
        },
        {
            levelId: 3,
            train: {
                normal: '硬席（硬座、硬卧）',
                middle: '二等座',
                high: '二等软座'
            },
            ship: '三等舱',
            plane: '经济舱'
        }
    ]
};
var province;
var city;
var district;
var startDate;
var endDate;
var cause;
var level
$(function () {

    layui.use(['layer'],function (e) {

    })
    for(var i = 0;i < app.provinceList.length; ++i){
        $("#province").append(
            '<option>'+ app.provinceList[i] +'</option>'
        );
    }
    for(var i = 0;i < app.cityList.length; ++i){
        $("#city").append(
            '<option>'+ app.cityList[i] + '</option>'
        );
    }
});
function provinceChange() {
    var province = $("#province").val();
    if(province != '陕西'){
        $("#city").addClass("cityClass")
    }else{
        $("#city").removeClass("cityClass");
    }
}
function commit1() {
    province = $("#province").val();   //省
    city = $("#city").val();   //市
    district = $("#district").val();   //区,县
    startDate = $("#startDate").val();  //出差开始日
    endDate = $("#endDate").val();  //出差结束日
    cause = $("#cause").val();  //出差原因
    level = $("#level").val();  //出差级别
    if(!checkTime(startDate,endDate)){
        endDate = startDate;
        $("#endDate").val(endDate);
    }
    var trafficStandard = trafficStandardLevel(level);  //交通标准
    var foodMoney = foodTrafficMoney(startDate,endDate,cause);  //伙食费补助
    var money = stayMoney(level,province,city);   //住宿标准
    var timeDay = getTimeDay(startDate,endDate);
    $.ajax({
        url: "/addTravel",
        type: "post",
        data: {
            "province": province,
            "city": city,
            "district": district,
            "startDate": startDate,
            "endDate": endDate,
            "cause": cause,
            "level": level,
            "timeDay": timeDay,
            "train": JSON.stringify(trafficStandard.train),
            "ship": trafficStandard.ship,
            "plane": trafficStandard.plane,
            "foodMoney": JSON.stringify(foodMoney.foodMoney),
            "trafficMoney": JSON.stringify(foodMoney.trafficMoney),
            "stayMoney": JSON.stringify(money)
        },
        success: function(res){
            if(res.flag){
                // layer.msg(res.message,{time:1000, icon:6, shift:6});
                setTimeout(function () {
                    window.location.href="/details/"+res.t;
                },1000);
            }else {
                layer.msg(res.message,{time:1000, icon:5, shift:6});
            }
        }
    })
}

function stayMoney(level,province,city){
    var levelType;//职务级别
    switch (level) {
        case '1':
            levelType='provinceLevel';
            break;
        case '2':
            levelType='cityLevel';
            break;
        case '3':
            levelType='normalLevel';
            break;
    }
    var firstType = province == '陕西' ? '省内':'省外';//第一列，区分省内还是省外
    var standard;//淡季住宿标准
    var standardSpecial;//旺季住宿标准
    var scale;//旺季上浮比例
    var time;//出差总天数（旺季出差天数+淡季出差）
    var specialMonth = [];//特殊月份
    if (firstType == app.provinceType[0]){//选择的是省内
        for (var i = 0; i < app.cityStayStandardList.length; i++) {
            var item = app.cityStayStandardList[i];//获取列表中的标准项
            if (city == item.city){//城市对应正确
                standard = item.standard;
                specialMonth=[];//设置特殊月份为空，代表没有特殊月份
            }
        }
    }else{//选择的是省外
        for (var i = 0; i < app.provinceStayStandardList.length; i++) {
            var item = app.provinceStayStandardList[i];//获取列表中的标准项
            if (province == item.province) {//如果省份一致
                standard = item.standard;
                if (item.type == 0) {//全省正常省份
                    specialMonth = [];
                } else if (item.type == 1) {//有特殊省份
                    specialMonth = item.months; //获取旺季涉及到的月份列表
                    standardSpecial = item.standardSpecial
                    scale = item.scale;//获取上浮比例
                } else {//有特殊城市
                    if (city == item.city) { //如果城市一致
                        specialMonth = item.months;
                        standard = item.standardCity;//选择特殊城市的淡季住宿标准
                        standardSpecial = item.standardCitySpecial;
                        scale = item.scale;
                    } else {
                        specialMonth = [];
                    }
                }
            }
        }
    }
    time = dateStatistics(specialMonth);
    var normalMoney = standard[levelType]*time.countNormal;//淡季的住宿补贴
    var specialMoney=0;//旺季的住宿补贴
    if (time.countSpecial != 0 && standardSpecial){//有旺季出差经历
        specialMoney = standardSpecial[levelType] * time.countSpecial;
    }
    var totalMoney = normalMoney + specialMoney;
    var staysMoney={
        value:totalMoney,//总的住宿补贴
        standard: standard[levelType],//淡季出差标准
        standardSpecial: standardSpecial?standardSpecial[levelType]:0,//旺季出差标准
        specialMonth: specialMonth,//旺季的特殊月份
        totalDay: time
    }
    return staysMoney;
}
function dateStatistics(time){//传入参数为月份列表 如[6,7,8]
    var start_date = startDate;
    var end_date = endDate;
    var list = getYearAndMonthAndDay(start_date, end_date);
    var countSpecial = 0;
    for (var i = 0; i < list.length; i++) {
        var day = list[i]
        var arr = day.split("-");
        for(var j=0;j<time.length;j++){
            if (arr[1]==time[j]){
                countSpecial++;
            }
        }
    }
    var countTotal = this.getTimeDay(startDate,endDate);
    var countNormal = countTotal - countSpecial;

    return {
        countTotal: countTotal,//总出差天数
        countSpecial: countSpecial,//旺季出差天数
        countNormal: countNormal//淡季出差天数
    }
}
function getYearAndMonthAndDay(start, end) {
    var date_all = [],
        i = 0;
    var startTime = new Date(start);
    var endTime = new Date(end);
    while ((endTime.getTime() - startTime.getTime()) >= 0) {
        var year = startTime.getFullYear();
        var month = (startTime.getMonth() + 1).toString().length == 1 ? "0" + (startTime.getMonth() + 1).toString() : (startTime.getMonth() + 1).toString();
        var day = startTime.getDate().toString().length == 1 ? "0" + startTime.getDate() : startTime.getDate();
        date_all[i] = year + "-" + month + "-" + day;
        startTime.setDate(startTime.getDate() + 1);//开始时间+1
        i += 1;
    }
    return date_all;
}

function checkTime(startDate, endDate) { //传入值格式2020-4-9
    var arr = startDate.split("-"); //获取年月日
    var starttime = new Date(arr[0], arr[1], arr[2]);
    var starttimes = starttime.getTime(); //获取时间戳
    var arrs = endDate.split("-");
    var endtime = new Date(arrs[0], arrs[1], arrs[2]);
    var endtimes = endtime.getTime();
    return endtimes > starttimes;
}

function foodTrafficMoney(startDate,endDate,cause) {
    var foodDays = getTimeDay(startDate,endDate); //伙食补助的天数
    var trafficDays = foodDays;
    var provinceType =  province == '陕西' ? '省内':'省外'; //获取是省内 还是省外
    if (cause == 1 || cause == 2) { //如果出差事由是培训、会议
        //只有第1天、最后1天有补助（也就是出差2天以上只补助2天）
        if (trafficDays >= 2) {
            trafficDays = 2;
        }
    } //其他情况每天都有交通补贴
    var foodMoney;
    var trafficMoney;
    if (provinceType == app.provinceType[0]) { //判断是省内
        foodMoney = { //伙食补助和补助标准
            value: foodDays * app.foodStandardList[0].value,
            standard: app.foodStandardList[0].value
        };
        trafficMoney = { //交通补助和补助标准
            value: trafficDays * app.trafficMoneyList[0].value,
            standard: app.trafficMoneyList[0].value
        };
    } else { //判断是省外
        foodMoney = {
            value: foodDays * app.foodStandardList[1].value,
            standard: app.foodStandardList[1].value
        };
        trafficMoney = {
            value: trafficDays * app.trafficMoneyList[1].value,
            standard: app.trafficMoneyList[1].value
        };
    }
    return {
        foodMoney: foodMoney,
        trafficMoney: trafficMoney
    };
}

function getTimeDay(startDate,endDate) {
    var s1 = startDate;
    s1 = new Date(s1.replace(/-/g, "/"));
    var s2 = endDate;
    s2 = new Date(s2.replace(/-/g, "/"));
    var days = s2.getTime() - s1.getTime();
    var time = parseInt(days / (1000 * 60 * 60 * 24)) + 1;
    return time;
}

function trafficStandardLevel(level) {
    var trafficStandard;
    for (var i = 0; i < app.trafficStandardList.length; i++) {
        var item = app.trafficStandardList[i];
        if (level == item.levelId) {
            trafficStandard = item;
        }
    }
    return trafficStandard;
}